package org.example.Polls;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class SOAPPollService implements PollService {
	private URL serviceUrl;
	
	public SOAPPollService(String url) throws MalformedURLException {
		serviceUrl = new URL(url);
	}

	public Poll createPoll(RequestContext ctxt, Poll poll) throws Exception {
		XStream xstream = XStreamManager.getInstance().getXStream();
		System.out.println(xstream.toXML(poll));
		Object[] values = {ctxt.getUsername(), ctxt.getPassword(), xstream.toXML(poll)};
		/*
		Object[] values = {ctxt.getUsername(),
		                   ctxt.getPassword(),
		                   poll.getTitle(), 
		                   poll.getAnonymous(),
		                   poll.getDynamic(), 
		                   poll.get_public(), 
		                   SOAPDateFormatter.getInstance().format(poll.getStartDate()),
		                   SOAPDateFormatter.getInstance().format(poll.getEndDate()),
		                   xstream.toXML(poll.getParticipants()),
		                   xstream.toXML(poll.getItems())};
		 */
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "create_poll", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List pollList = unmarshaller.unmarshal("//poll");
		if (pollList == null || pollList.size() == 0 || !(pollList.get(0) instanceof Poll))
			throw new Exception("Unknown response received!");
		return (Poll)pollList.get(0);
	}

	public Poll getPoll(RequestContext ctxt, Long pollId,
			boolean participantDetails, boolean itemDetails) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword(), pollId, participantDetails, itemDetails};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "get_poll", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List pollList = unmarshaller.unmarshal("//poll");
		if (pollList == null || pollList.size() == 0 || !(pollList.get(0) instanceof Poll))
			throw new Exception("Unknown response received!");
		return (Poll)pollList.get(0);
	}

	public PollParticipant vote(RequestContext ctxt, Long pollId, Long pollItemId) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword(), pollId, pollItemId};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "vote", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List participantList = unmarshaller.unmarshal("//participant");
		if (participantList == null || participantList.size() == 0 || !(participantList.get(0) instanceof PollParticipant))
			throw new Exception("Unknown response received!");
		return (PollParticipant)participantList.get(0);
	}

	public PollParticipant addParticipant(RequestContext ctxt, String email, Long pollId) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword(), email, pollId};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "add_participant", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List participantList = unmarshaller.unmarshal("//participant");
		if (participantList == null || participantList.size() == 0 || !(participantList.get(0) instanceof PollParticipant))
			throw new Exception("Unknown response received!");
		return (PollParticipant)participantList.get(0);
	}

	public PollItem addItem(RequestContext ctxt, String itemValue, Long pollId) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword(), itemValue, pollId};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "add_item", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List itemList = unmarshaller.unmarshal("//item");
		if (itemList == null || itemList.size() == 0 || !(itemList.get(0) instanceof PollItem))
			throw new Exception("Unknown response received!");
		return (PollItem)itemList.get(0);
	}

	@Override
	public List<Poll> findMyPolls(RequestContext ctxt, Date start, Date end) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword()};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "find_my_polls", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List pollList = unmarshaller.unmarshal("//poll");
		if (pollList == null)
			throw new Exception("Unknown response received!");
		return (List<Poll>)pollList;
	}

	@Override
	public List<Poll> findParticipantPolls(RequestContext ctxt, Date start, Date end) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword()};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "find_participant_polls", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List pollList = unmarshaller.unmarshal("//poll");
		if (pollList == null)
			throw new Exception("Unknown response received!");
		return (List<Poll>)pollList;
	}

	@Override
	public List<Poll> findPublicPolls(RequestContext ctxt, Date start, Date end) throws Exception {
		Object[] values = {ctxt.getUsername(), ctxt.getPassword()};
		SOAPHelper soapHelper = SOAPHelper.getInstance();
		String response = soapHelper.sendRequest(serviceUrl, "find_public_polls", values);
		SOAPUnmarshaller unmarshaller = new SOAPUnmarshaller(response);
		List pollList = unmarshaller.unmarshal("//poll");
		if (pollList == null)
			throw new Exception("Unknown response received!");
		return (List<Poll>)pollList;
	}

}
